package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanScodeRecord.LOAN_SCODE_RECORD;

import org.apache.commons.lang3.StringUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.family.MobileCodeSendRecordRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.MobileCodeSendRecordSearchRequest;
import com.tuanzi.loan.vo.response.family.MobileCodeSendRecordSearchResponse;

@Repository
public class MobileCodeSendRecordRepositoryImpl extends JOOQBaseRepository implements MobileCodeSendRecordRepository {
    /**
     * MobileCodeSendRecord查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<MobileCodeSendRecordSearchResponse> list(MobileCodeSendRecordSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_SCODE_RECORD.fields());
        eq(LOAN_SCODE_RECORD.MOBNUM,searchRequest.getPhoneNumber());
        ge(LOAN_SCODE_RECORD.CREATETIME,searchRequest.getStartTime());
        le(LOAN_SCODE_RECORD.CREATETIME,searchRequest.getEndTime());
        orderBy(LOAN_SCODE_RECORD.ID, Sort.Direction.DESC);
        return search(pageable, record -> {
            MobileCodeSendRecordSearchResponse response = new MobileCodeSendRecordSearchResponse();
            response.setId(record.get(LOAN_SCODE_RECORD.ID));
            response.setPk(record.get(LOAN_SCODE_RECORD.ID));
            response.setCreateTime(record.get(LOAN_SCODE_RECORD.CREATETIME));
            
            String mobnum = record.get(LOAN_SCODE_RECORD.MOBNUM);
            if(StringUtils.isNotEmpty(mobnum)) {
            	response.setPhoneNumber(mobnum.substring(0, 3)+"****"+mobnum.substring(7, 11));
            }
            response.setCode(record.get(LOAN_SCODE_RECORD.S_CODE));
            response.setPlatform(record.get(LOAN_SCODE_RECORD.PLATFORM));
            response.setPrdId(record.get(LOAN_SCODE_RECORD.PRDID));
            response.setChannel(record.get(LOAN_SCODE_RECORD.CHANNEL));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_SCODE_RECORD;
    }
}
